package com.github.tangmonkmeat;

import java.lang.reflect.Proxy;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/7/22 上午10:56
 */
public class 买卖股票的最佳时机 {

    public static void main(String[] args) {

        int[] pri = {0,100};

        System.out.println(maxProfit2(pri));

    }

    // 7,1,5,3,6,4
    // 5
    public static int maxProfit(int[] prices) {
        // i 天前的最大利润
        int[] dp = new int[prices.length];
        int cur = 0;
        for (int i = 0;i < dp.length;i++){
            for (int j = 0;j < i;j++){
                dp[j] = Math.max(cur,dp[j]);
                cur = Math.max(prices[i] - prices[j],dp[j]);
            }
            dp[i] = cur;
        }
        return dp[dp.length - 1];
    }

    public static int maxProfit2(int[] prices){
        int min = Integer.MAX_VALUE;
        int max_pro = 0;

        for (int price : prices) {
            if (price < min) {
                min = price;
            } else if (price - min > max_pro) {
                max_pro = price - min;
            }

        }
        return max_pro;
    }
}
